package com.atguigu.gulimall.product.bn.controller;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URL;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 企业微信请求专用
 * @author chunyv
 */
public class HttpClientProxy implements Runnable {

    private static String proxyHost = "10.1.13.120"; //安全认证平台 ip
    private static Integer proxyPort = 8085;//安全认证平台 port
    private static String userName = "crcl-app-zttoaxt";//用户名
    private static String passWord = "Zttoaxt@123";//密码
    private static String http = "http";
    private static String dmzUrl = "http://zttoaxt.china-railway.com.cn:8080";//dmz服务地址
    private static String sendMessageUrl = "/weixin/sendmessage";//企业微信发送消息url

    /**
     * 企业微信发送消息
     * @throws IOException
     */
    public static void doGet(String userCode,String contentMsg) throws IOException {
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(proxyHost, proxyPort),
                new UsernamePasswordCredentials(userName, passWord));
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider).build();
        try {
            URL url = new URL(dmzUrl);
            HttpHost target = new HttpHost(url.getHost(), url.getPort(), http);
            HttpHost proxy = new HttpHost(proxyHost, proxyPort, http);
            RequestConfig config = RequestConfig.custom()
                    .setProxy(proxy)
                    .build();
            //OAInterface/weixin/sendmessage?userCode=HaoJie&contentMsg=
            //"您收到1条新的待办公文，请及时到公文管理系统中处理。"
            String param = "?userCode="+userCode+"&contentMsg="+contentMsg;
            HttpGet request = new HttpGet(sendMessageUrl+param);
            request.setConfig(config);
            System.out.println("Executing request " + request.getRequestLine() + " to " + target + " via " + proxy);
            CloseableHttpResponse response = httpclient.execute(target, request);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                System.out.println(EntityUtils.toString(response.getEntity()));
            } finally {
                response.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            httpclient.close();
        }
    }


    public static void main(String[] args) throws IOException {
        String param = "HaoJie";
        doGet(param,"test");
    }


    @Override
    public void run() {
        try {
            doGet(userCode,contentMsg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private String userCode;
    private String contentMsg;

    public HttpClientProxy(String userCode,String contentMsg) {
        this.userCode = userCode;
        this.contentMsg = contentMsg;
    }
}
